/**
 * Created by dqw on 2015/12/30.
 */

//列表开始
var dtGridColumns = [
    {
        id: 'delFlag',
        type: 'int',
        hideQuery:true,
        'export':false,
        hideQueryType:'eq',
        hideQueryValue:1,
        hide:true

    },
    {
        id: 'status',
        type: 'int',
        hideQuery:true,
        'export':false,
        hideQueryType:'eq',
        hideQueryValue:2,
        hide:true

    },
    {
        id: 'id',
        title: '编号',
        type: 'int',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false
    },
    {
        id: 'u.userName',
        title: '用户名',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'orderNo',
        title: '策略号',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'agentName',
        title: '代理商',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'name',
        title: '品种',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'direction',
        title: '方向',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'eq',
        codeTable: {
            1: $lang.OrderDirection.T1,
            2: $lang.OrderDirection.T2
        }
    },
    {
        id: 'settleWay',
        title: '委托方式',
        type: 'int',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'eq',
        codeTable: {
            1: $lang.settleWay.T1,
            2: $lang.settleWay.T2
        }
    },
    {
        id: 'number',
        title: '数量（手）',
        type: 'int',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: false
    },
    {
        id: 'entrustPrice',
        title: '委托价格',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false
    },
    {
        id: 'synthesisFee',
        title: '交易综合费',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false,
        hideType: 'md|sm|xs|lg'
    },
    {
        id: 'deposit',
        title: '保证金',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false
    },
    {
        id: 'profit',
        title: '实际盈亏',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false
    },
    {
        id: 'profitStopPrice',
        title: '止盈',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false,
        hideType: 'md|sm|xs|lg'
    },
    {
        id: 'loseStopPrice',
        title: '止损',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false,
        hideType: 'md|sm|xs|lg'
    },
    {
        id: 'status',
        title: '状态',
        type: 'int',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'eq',
        codeTable: {
            1: $lang.orderStatus.T1,
            2: $lang.orderStatus.T2,
            3: $lang.orderStatus.T3,
            4: $lang.orderStatus.T4,
            5: $lang.orderStatus.T5,
            6: $lang.orderStatus.T6,
            7: $lang.orderStatus.T7,
            8: $lang.orderStatus.T8,
            9: $lang.orderStatus.T9,
            10: $lang.orderStatus.T10
        }
    },
    {
        id: 'createTime',
        title: '发起时间',
        type: 'date',
        format: 'yyyy-MM-dd hh:mm:ss',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'range'
    },
    {
        id: 'openBusiNo',
        title: '开仓成交编号',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: false,
        hideType: 'md|sm|xs|lg'
    },
    {
        id: 'settleBusiNo',
        title: '平仓成交编号',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: false,
        hideType: 'md|sm|xs|lg'
    },
    {
        id: 'topProfitUpdateTime',
        title: '止盈止损修改时间',
        type: 'date',
        format: 'yyyy-MM-dd hh:mm:ss',
        headerClass: 'text-center',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'range',
        hideType: 'md|sm|xs|lg',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            if(record.topProfitUpdateTime== undefined){
                return "--";
            }else{
                return record.topProfitUpdateTime;
            }
        }
    }
    // {
    //     id: 'operation',
    //     title: '操作',
    //     type: 'string',
    //     'export':false,
    //     headerClass: 'text-center',
    //     columnClass: 'text-center',
    //     fastSort: false,
    //     resolution: function (value, record, column, grid, dataNo, columnNo) {
    //         var html="",
    //             cancel="",
    //             taking="";
    //         if ($("#orderCancel") && $("#orderCancel").val() == 1) {
    //             cancel = "<a href='javascript:;' class='btn btn-warning btn-sm m-r-10' onclick='OperateHandle.cancelOrder(" + record.id + ",\"" + record.orderNo + "\")'><i class='fa fa-reply-all'></i>&nbsp;撤单&nbsp;</a>";
    //         }
    //         if ($("#orderEntrust") && $("#orderEntrust").val() == 1) {
    //             taking = "<a data-target='#editModal' class='btn btn-sm btn-primary m-r-10' data-toggle='modal' data-no='" + dataNo + "' ><i class='fa fa-thumbs-up'></i>&nbsp;开仓&nbsp;</a>";
    //         }
    //         if(record.status == 2){
    //             html = cancel+taking;
    //         }
    //         return html;
    //     }
    // }
];

var dtGridOption = {
    lang: 'zh-cn',
    ajaxLoad: true,
    loadURL: ncGlobal.adminRoot + 'order/json/entrust',
    exportURL: ncGlobal.adminRoot + 'order/json/export',
    exportFileName: '策略委托中',
    columns: dtGridColumns,
    gridContainer: 'dtGridContainer',
    toolbarContainer: 'dtGridToolBarContainer',
    tools: 'refresh|faseQuery|export[excel]',
    pageSize: 10,
    pageSizeLimit: [10, 20, 50],
    ncColumnsType: {bigDecimal:["amount",'totalLeft'],int:["id","type"],Timestamp: ["createTime"]}
};

var grid = $.fn.DtGrid.init(dtGridOption);
grid.fastQueryParameters = new Object();
grid.fastQueryParameters['eq_delFlag'] = 1;
grid.fastQueryParameters['eq_status'] = 2;
//排序
grid.sortParameter.columnId = ['desc_createTime'];
//列表结束

//操作处理开始
var OperateHandle = function () {

    function _bindEvent() {
        //模糊搜索
        $('#customSearch').click(function () {
            grid.fastQueryParameters = new Object();
            grid.fastQueryParameters['eq_delFlag'] = 1;
            grid.fastQueryParameters['eq_status'] = 2;
            grid.fastQueryParameters['lk_u.user_name_or_lk_orderNo_or_lk_ag.name'] = $('#keyword').val();
            grid.pager.startRecord = 0;
            grid.pager.nowPage = 1;
            grid.pager.recordCount = -1;
            grid.pager.pageCount = -1;
            grid.refresh(true);
        });
    }

    /**
     * 撤单
     */
    function _cancelOrder(id, orderNo) {
        var tpl = '策略号：'+orderNo+' 确定要撤单？'
        $.ncConfirm({
            url: ncGlobal.adminRoot + "order/json/cancel",
            data: {
                id: id
            },
            content: tpl,
            alertTitle: "撤单操作"
        });
    }

    //平仓对话框初始化
    $("#editModal").on("show.bs.modal", function (event) {
        //清除错误信息
        $(".alert-danger").remove();
        $("#editForm").psly().reset();
        //获取接受事件的元素
        var button = $(event.relatedTarget);
        //获取data 参数
        var datano = button.data('no');
        //获取列表框中的原始数据
        var gridData = grid.sortOriginalDatas[datano];
        $editForm = $("#editForm");
        $editForm.find('input[name="id"]').val(gridData.id);
        $editForm.find('input[name="openAmount"]').val('');
        $editForm.find('input[name="openPrice"]').val('');
        $editForm.find('input[name="openBusiNo"]').val('');
        $editForm.find('input[name="openTime"]').val('');
        //清除错误提示
        $(".alert-danger").remove()
    });

    //外部可调用
    return {
        init: function () {
            _bindEvent();
        },
        cancelOrder: _cancelOrder,
    }
}();

$(function () {
    //加载列表
    grid.load();
    //页面绑定事件
    OperateHandle.init();
})